<%@page import="com.liferay.portal.model.ListTypeConstants"%>
<%@page import="com.slayer.model.impl.LMSBookImpl"%>
<%@include file="/html/library/init.jsp"%>

<%
	boolean isPopup = ParamUtil.getBoolean(request, "isPopup",false);
	PortletURL updateBookURL = renderResponse.createActionURL();
	updateBookURL.setParameter(ActionRequest.ACTION_NAME, LibraryConstants.ACTION_UPDATE_BOOK);
	
	LMSBook lmsBook = new LMSBookImpl();
	long bookId = ParamUtil.getLong(request, "bookId");
	
	if (bookId > 0l) {
		lmsBook = LMSBookLocalServiceUtil.fetchLMSBook(bookId);
	}	
%>

<c:choose>
	<c:when test="<%=!isPopup %>">
		<h1>Add/Update Book</h1>
	</c:when>
</c:choose>

<aui:form name="fm" action="<%= updateBookURL.toString() %>" target="_parent" method="post">
	
	<aui:input name="bookId" type="hidden" value="<%= lmsBook.getBookId() %>"/>
	<aui:input name="bookTitle" label="Book Title" value="<%= lmsBook.getBookTitle() %>" >
		<aui:validator name="" errorMessage=""/>
	</aui:input>
	<aui:input name="author" helpMessage="Give the Author Name" value="<%= lmsBook.getAuthor() %>" />
	<%@include file="/html/library/address.jspf" %>
	<aui:button type="submit" />
	<aui:button value="Save Through JSON" onClick="javascript:saveBook();"/>	
</aui:form>

<c:choose>
	<c:when test="<%=!isPopup %>">
		<br/><a href="<portlet:renderURL/>">&laquo; Go Back</a>
	</c:when>
</c:choose>	
<aui:script>
	function saveBook() {
		var fm = document.<portlet:namespace/>fm;
		
		Liferay.Service(
		  '/library-portlet.lmsbook/inser-book',
		  {
		    bookTitle: fm.<portlet:namespace/>bookTitle.value,
		    author: fm.<portlet:namespace/>author.value
		  },
		  function(obj) {
			  fm.<portlet:namespace/>bookTitle.value = '';
			  fm.<portlet:namespace/>author.value = '';
		  }
		);
	}
	
</aui:script>	